package com.tuishan.myapplication.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tuishan.myapplication.Custom.LoadingView;
import com.tuishan.myapplication.Model.OrderModel;
import com.tuishan.myapplication.Model.WalletModel;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.List;
import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

/**
 * <pre>
 *     time   : 2021/04/23
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class WalletAdapter extends RecyclerView.Adapter<WalletAdapter.ViewHolder> {

    private List<WalletModel> mProductList;

    //1生活 2学习 3服饰 4食品 5家具 6其他
    String[] TypeList = new String[]{
            "", "生活", "学习", "服饰", "食品", "家具", "其他"
    };
    //1全新 2几乎全新 3轻微使用 4正常使用 5明显使用
    String[] UsageList = new String[]{
            "", "全新", "几乎全新", "轻微使用", "正常使用", "明显使用"
    };

    //1待审批 2发布 3交易中 4卖出 5下架
    String[] ResultList = new String[]{
            "", "待审批", "发布", "交易中", "卖出", "下架"
    };

    LoadingView Loading;
    Context mContext;
    ReleaseCall releaseCall;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title_name, title_num, title_message, title_use, title_type, title_Result;
        Button home_collection_btn, home_buy_btn;
        SimpleDraweeView product_image;

        public ViewHolder(View view) {
            super(view);
            title_name = view.findViewById(R.id.title_name);
            title_num = view.findViewById(R.id.title_num);
            title_use = view.findViewById(R.id.title_use);
            title_type = view.findViewById(R.id.title_type);
            title_Result = view.findViewById(R.id.title_Result);
            title_message = view.findViewById(R.id.title_message);
            home_collection_btn = view.findViewById(R.id.home_collection_btn);
            home_buy_btn = view.findViewById(R.id.home_buy_btn);
            product_image = view.findViewById(R.id.product_image);
            title_Result.setVisibility(View.VISIBLE);
        }

    }

    public WalletAdapter(List<WalletModel> productModelList, LoadingView arg1, Context arg2, ReleaseCall call) {
        this.mProductList = productModelList;
        this.Loading = arg1;
        this.mContext = arg2;
        this.releaseCall = call;
    }

    @NonNull
    @Override
    public WalletAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_home_product, parent, false);
        WalletAdapter.ViewHolder holder = new WalletAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull WalletAdapter.ViewHolder holder, int position) {
        WalletModel productModel = mProductList.get(position);
        holder.product_image.setImageURI(productModel.getPhoto());
        holder.title_name.setText("名字：" + productModel.getName());
        holder.title_num.setText("价格：" + productModel.getAccount());
        holder.title_message.setText("描述：" + productModel.getRemarks());
        holder.title_use.setText("新旧：" + UsageList[productModel.getUsage()]);
        holder.title_type.setText("类型：" + TypeList[productModel.getClassType()]);
        holder.title_Result.setText("状态：" + ResultList[productModel.getResult()]);
        holder.home_collection_btn.setText("下架商品");
        holder.home_buy_btn.setText("删除商品");
        holder.home_collection_btn.setOnClickListener(view -> CancelOrder(productModel.getId()));
        holder.home_buy_btn.setOnClickListener(view -> ConfirmOrder(productModel.getId()));
    }

    @Override
    public int getItemCount() {
        return mProductList.size();
    }

    //删除订单
    private void ConfirmOrder(int productId) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("productId", productId);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.GetDeleteTransactUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                ToastUtils.showShort("删除成功");
                releaseCall.callRelease();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }

    //下架订单
    private void CancelOrder(int productId) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("productId", productId);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.GetOfflineTransactUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                ToastUtils.showShort("下架成功");
                releaseCall.callRelease();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }

    public interface ReleaseCall {
        void callRelease();
    }
}
